package com.wandian.core
{

	import com.wandian.core.allUtils.UILibrary;
	import com.wandian.util.Res;

	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	/**
	 * 加载资源类，如swf
	 * @author long
	 */
	public class LoadUIManager extends EventDispatcher
	{
		private var loaderContext:LoaderContext=new LoaderContext
		private var _str:String
		private var _fun:Function

		public function LoadUIManager()
		{
			super();
		}
		private static var _instance:LoadUIManager;

		public static function get instance():LoadUIManager
		{
			return _instance||=new LoadUIManager;
		}

		public function load(str:String, fun:Function=null):void
		{
			_fun=fun
			_str=str
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadOk)
			loader.load(new URLRequest("assests/" + str + ".swf"), loaderContext)
		}

		private function loadOk(e:Event):void
		{
			var loader:Loader=(e.currentTarget as LoaderInfo).loader
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadOk)
			ClassCreatManager.instance.appDomain=loaderContext.applicationDomain
			Res.init(loaderContext.applicationDomain);
			UILibrary.instance.setSwfDic(loader.contentLoaderInfo, _str)
			dispatchEvent(new Event(Event.COMPLETE));
			if (_fun != null)
			{
				_fun();
			}
		}

	}
}
